﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.IO;
using Albon.Model;
using Commonlib;
namespace AlbonWeb.Manager.ClientManager
{
    public partial class PostureData : System.Web.UI.Page
    {
        private readonly BLL.ClientBLL clientbll = new BLL.ClientBLL();
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            { 
                //绑定身形类别
                string cmdstr = "select * from PosturePicture where parentID=0";
                SqlHelper.BindDropDownList(ddlParent, "postureName", "ID", SqlHelper.SqlConnectionString, CommandType.Text, cmdstr, null);
                ddlParent.Items.Insert(0, new ListItem("--the top--","0"));
                if (Request["id"] != null)
                {
                    initData();   
                }
                //绑定数据 
                bindData();
            }
        }
        //绑定身形图片数据
        protected void bindData()
        {
            string cmdstr = "select * from PosturePictureView order by createDate desc";
            SqlHelper.BindRepeater(repMain, SqlHelper.SqlConnectionString, CommandType.Text, cmdstr, null);
        }

        //初始化数据
        protected void initData()
        {
            DataTable dt=clientbll.getPosturePictureByID(Request["id"].ToString());
            if (dt.Rows.Count != 0)
            {
                postureName.Text = dt.Rows[0]["postureName"].ToString();
                ddlParent.SelectedValue = dt.Rows[0]["parentID"].ToString();
                hffilepath.Value = dt.Rows[0]["imgPath"].ToString();
                remark.Text = dt.Rows[0]["remark"].ToString();
            }
        }

        // 添加/修改 身形数据
        protected void btnAdd_Click(object sender, EventArgs e)
        {
            string filepath = "";
            if (FileUpload1.HasFile)
            {
                FileUpload1.SaveAs(Server.MapPath("~/") + @"\Manager\ClientManager\PosturePicture\" + FileUpload1.FileName);
                filepath =@"\Manager\ClientManager\PosturePicture\" + FileUpload1.FileName;
            }
            else
            {
                filepath = hffilepath.Value;
            }
            PosturePicture posture = new PosturePicture();
            if (Request["id"] != null)
            {
                posture.ID = int.Parse(Request["id"].ToString());
            }
            else
            {
                posture.ID = 0;
            }
            posture.postureName = postureName.Text.Trim();
            posture.parentID =int.Parse(ddlParent.SelectedValue);
            posture.imgPath = filepath;
            posture.remark = remark.Text.Trim();
            clientbll.addPosturePicture(posture);
            bindData();
            
        }

        protected  void imgDelete_Click(object sender, ImageClickEventArgs e)
        {
            string id = ((ImageButton)sender).CommandArgument;
            if (clientbll.delPosturePicture(id))
            {
                bindData();
            }
        }
    }
}